import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

class TranslationService extends Translations {
  // Default locale
  static const defaultLocale = Locale('en', 'US');

  // 中文Locale
  static const chineseLocale = Locale('zh', 'CN');

  // Fallback locale
  static const fallbackLocale = Locale('en', 'US');

  // Supported languages
  static final languages = [
    'en_US',
    'zh_CN',
    // Add more languages as needed
  ];

  // Supported locales
  static final locales = [
    const Locale('en', 'US'),
    const Locale('zh', 'CN'),
    // Add more locales as needed
  ];

  // Keys for storing and retrieving the selected language from SharedPreferences
  static const String _kLanguageCode = 'languageCode';
  static const String _kCountryCode = 'countryCode';

  @override
  Map<String, Map<String, String>> get keys => {
        'en_US': {
          'hello': 'Hello',
          'settings': 'Settings',
          // Add more translations
        },
        'zh_CN': {
          'hello': '你好',
          'settings': '设置',
          // Add more translations
        },
        // Add more language translations
      };

  // Gets the locale from SharedPreferences
  static Locale getLocale() {
    final languageCode = SharedPreferencesService.to.getString(_kLanguageCode);
    final countryCode = SharedPreferencesService.to.getString(_kCountryCode);
    return Locale(languageCode ?? 'en', countryCode ?? 'US');
  }

  // Saves the selected locale to SharedPreferences
  static Future<void> saveLocale(Locale locale) async {
    await SharedPreferencesService.to
        .setString(_kLanguageCode, locale.languageCode);
    await SharedPreferencesService.to
        .setString(_kCountryCode, locale.countryCode ?? '');
  }

  // Changes the app's language
  static Future<void> changeLocale(Locale locale) async {
    await saveLocale(locale);
    Get.updateLocale(locale);
  }
}
